package git.soulbgm.common.exception;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 参数为空异常
 *
 * @author SoulBGM
 * @version 1.0
 * @date 2019/9/23 16:46
 */
public class ParamIsNullException extends RuntimeException {

    private String parameterName;
    private String parameterType;
    private Map<String, List<String>> errorMap;

    public ParamIsNullException(String parameterName, String parameterType) {
        this.parameterName = parameterName;
        this.parameterType = parameterType;
    }

    public ParamIsNullException(Map<String, List<String>> errorMap) {
        this.errorMap = errorMap;
    }


    @Override
    public String getMessage() {
        if (errorMap != null) {
            Set<String> key = errorMap.keySet();
            StringBuilder sb = new StringBuilder();
            sb.append("需要");
            for (String k : key) {
                sb.append(" ");
                sb.append(k);
                sb.append(" 下的");
                List<String> list = errorMap.get(k);
                for (String s : list) {
                    String[] split = s.split(":");
                    sb.append(" ");
                    sb.append(split[0]);
                    sb.append(" 参数 \'");
                    sb.append(split[1]);
                    sb.append("\',");
                }
                sb.deleteCharAt(sb.length() - 1);
                sb.append(";");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(" 不能为空");
            return sb.toString();
        } else {
            return "需要 " + this.parameterType + " 参数 \'" + this.parameterName + "\' 不能为空";
        }
    }

    public final String getParameterName() {
        return this.parameterName;
    }

    public final String getParameterType() {
        return this.parameterType;
    }

}
